%{
                                                       makePolarPlots.m V1
                                                     Written by Rajeev Kant
                                                        Sept. 4th, 2020
                                    Coulombe Lab for Cardiovascular Regenerative Engineering
                            Center for Biomedical Engineering, Brown Univeristy, Providence, RI, 02903
                                                

The purpose of this code is to plot average population outgrowth per
day from aortic rings in a polar plot with error bars. 

The user will need to edit lines 61 and 62 to point towards the correct spreadsheet

Inputs:
1. col: 7 x 1 Cell array of Excel ranges where each row corresponds to
average outgrowth distance per day.
2. col_err: 7 x 1 Cell array of Excel ranges containing error (SEM or STD)
corresponding to each measurement in col
4. condition: Character array for plot title and naming output.

Outputs:
1. cent: List of centroid coordinates for each day of outgrowth.
2. Polar plot image file
3. OPTIONAL: Quiverplot showing 7-day centroid shift

---
EXAMPLE:
col={'D4:D28';'E4:E28';'F4:F28';'G4:G28';'H4:H28';'I4:I28';'J4:J28'};
col_err={'D32:D56';'E32:E56';'F32:F56';'G32:G56'; 'H32:H56';'I32:I56';'J32:J56'};
E_cent = makePolarPlots(col, col_err, '- EC');
---


Original publication for citation: 
Kant, R. J., Bare, C. F., and Coulombe, K. L. K. "Tissues with 
patterned vessels or protein release induce vascular chemotaxis
in an in vitro platform", Tissue Eng. Part A, 2021.
doi:10.1089/ten.TEA.2020.0269

%}

function [cent] = makePolarPlots (col, col_err, condition)
%% Load in data from Excel spreadsheet and create polar plot

% Initialize variables
ang = zeros(25,1);
for i = 2:24
    ang(i,1) = ang(i-1,1) + 15;
end

angRad = deg2rad(ang); % Angles 0-345 in rad
len = length(angRad);
gro = zeros(25,7); % Outgrowth
err = zeros(25,7); % Standard Error
colors = [1 0 0; 1 .53 0; 1 1 0; 0 1 0; 0 0 1; .67 0 1; .5 0 .5]; % Color array for plotting

% Establish blank polar axes
figure;
base = polarplot(angRad, ones(len,1)); 
set(base, 'Visible', 'off');
hold on;

% Load in specified outgrowth and error data from Excel spreadsheet and
% plot each set of points
for i=1:7
    gro(:,i) = xlsread('ARA Repeat','Polar Plots', char(col(i))); % Edit filename and spreadsheet names here
    err(:,i) = xlsread('ARA Repeat','Polar Plots', char(col_err(i)));
    polarplot(angRad, gro(:,i), '-o', 'MarkerSize', 4,'LineWidth', 1.25, 'Color', colors(i,:)); % Plot outgrowth of ith day
    for k = 1 : len % For kth point in ith day of outgrowth, add in error bar
        polarplot(angRad(k) * ones(1,3), [gro(k,i) - err(k,i), gro(k,i), gro(k,i) + err(k,i)], '-k', 'LineWidth', 1.5);
    end
end

% Set polar plot options
title(strcat(condition, ' Outgrowth')); 
rlim([0 2500]);
ax=gca;
ax.FontSize = 16;
ax.GridAlpha = 0.4;
ax.RTickLabel = {''; ''; ''; ''; ''};
%ax.RTickLabel = {'0 mm'; '0.5 mm'; '1 mm'; '1.5 mm'; '2.5 mm'};
thetaticks(0:15:345);

% Save file, adding escape character for - if needed)
iptsetpref('ImshowBorder','tight');
if strcmp(condition, '- EC')
    saveas(gcf,strcat('./', condition, ' Outgrowth'),'tiff');
else
    saveas(gcf,strcat(condition, ' Outgrowth'),'tiff');
end

hold off;
close all;

%% Create polygonal maps from polar plots to calculate centroids

% Initialize variables
polyX=zeros(25,7); % Cartesian coordinates of each point in polar plot at each timepoint
polyY=zeros(25,7);
cent=zeros(7,2); % Centroids of pgon at each time point

for i=1:7 % Convert polar to cartesian and find centroids
    [polyX(:,i),polyY(:,i)] = pol2cart(angRad(1:25,1),gro(:,i));
    pgon(i,1)=polyshape(polyX(:,i),polyY(:,i));
    [cent(i,1), cent(i,2)]=centroid(pgon(i,1));
end

% Polygonal plots of cartesian coordinates at each timepoint if desired
%{
figure;
hold on
for i=7:-1:1
    plot(pgon(i),'FaceColor',colors(i,:), 'FaceAlpha', 1)
end
xlim([-2000 2000])
ylim([-2000 2000])
title(strcat(condition, ' Outgrowth'));
hold off;
%}
%% Create quiver plot of centroid shift

cent = [0 0; cent]; % Append origin to beginning of cent for vector calculations

% Initialize
vect=zeros(6,2); % Vectors between measured days
vectend=zeros(2,2); % Vector bewteen day 0 (0, 0) and day 1

for i=2:8 % Calculate vectors between centroid points
    vect(i-1,1) = cent(i,1) - cent(i-1,1);
    vect(i-1,2) = cent(i,2) - cent(i-1,2);
end

vectend(2,:) = cent(8,:); % Vector between origin and Day 7 centroid

figure;
hold on;

for i=1:7 % Quiver plots for each day
    quiver(cent(i,1), cent(i,2), vect(i,1), vect(i,2), 0)
end
colororder(colors)
quiver(vectend(1,1), vectend(1,2), vectend(2,1),vectend(2,2), 0, 'Color', 'k') % Overall quiver vector
title(strcat(condition, ' Centroid Shift'));
xlim([-150 150]);
ylim([-150 150]);

for i=1:8 % Plot centroids over quiver plot
    if i == 1
        plot(cent(i,1),cent(i,2), '*','Color','k');
    else
        plot(cent(i,1),cent(i,2), '*','Color',colors(i-1,:));
    end
end

% Save file, adding escape character for - if needed)

iptsetpref('ImshowBorder','tight');
if strcmp(condition, '- EC')
    saveas(gcf,strcat('./', condition, ' CentroidShift'),'tiff');
else
    saveas(gcf,strcat(condition, ' CentroidShift'),'tiff');
end
hold off;
close all;

end % End of makePolarPlots function

